package com.LeeCode;

import java.util.HashSet;

/**
 * 存在重复元素 II
 */

public class Code219 {
    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 1, 2, 3};
        int k = 2;
        System.out.println(new Code219().containsNearbyDuplicate(nums, k));
    }

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashSet<Integer> set = new HashSet<>();

        for (int i = 0; i < nums.length; i++) {
            if (set.contains(nums[i])) return true;
            set.add(nums[i]);
            if (i < k) continue;
            int pos = i - k;
            set.remove(nums[pos]);
        }
        return false;
    }
}
